package com.shen.pool;

import java.util.concurrent.*;

public class Demo02 {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                2,//核心线程数
                5,//最大线程数
                3,//空闲线程存活时间
                TimeUnit.SECONDS,//时间单位
                new LinkedBlockingDeque<>(3),//阻塞方式 双端队列
                Executors.defaultThreadFactory(),//线程工厂
                new ThreadPoolExecutor.AbortPolicy()
                //默认的拒绝策略，当线程池满载了，不能再接收更多的请求时，会报出异常信息 java.util.concurrent.RejectedExecutionException
        );


        try {
            for (int i = 0; i < 100; i++) {
                    //使用线程池创建线程
                    threadPoolExecutor.execute(()->{
                        System.out.println(Thread.currentThread().getName()+" ok ");
                    });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //线程池用完关闭线程池
            threadPoolExecutor.shutdown();
        }


    }
}
